<template>
  <el-form ref="form" :rules="rules" label-width="100px">
    <el-form-item label="步骤名称" prop="name">
      <el-input v-model="step.name" placeholder="步骤名称"></el-input>
    </el-form-item>
    <br>
    <el-button type="primary" @click="fetchFieldsFromInput">获取字段</el-button>
    <el-button icon="plus" @click="_addRecord"></el-button>
    <br><br>
    <el-table :data="step.fields" stripe border :max-height="400">
      <el-table-column label="输入流字段" width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.in_stream_name}}</div>
          <div v-else>
            <el-select v-model="scope.row.in_stream_name">
              <el-option v-for="item in step.availableFields" :value="item.name"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="输出流字段" width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.out_stream_name}}</div>
          <div v-else>
            <el-input v-model="scope.row.out_stream_name"></el-input>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="使用正则表达式" width="140">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.use_regex}}</div>
          <div v-else>
            <el-select v-model="scope.row.use_regex">
              <el-option value="yes"></el-option>
              <el-option value="no"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="搜索" width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.replace_string}}</div>
          <div v-else>
            <el-input v-model="scope.row.replace_string"></el-input>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="使用...替换" width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.replace_by_string}}</div>
          <div v-else>
            <el-input v-model="scope.row.replace_by_string"></el-input>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="设置为空串" width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.set_empty_string}}</div>
          <div v-else>
            <el-select v-model="scope.row.set_empty_string">
              <el-option value="Y"></el-option>
              <el-option value="N"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="使用字段值替换" width="160">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.replace_field_by_string}}</div>
          <div v-else>
            <el-select v-model="scope.row.replace_field_by_string">
              <el-option v-for="item in step.availableFields" :value="item.name"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="整个单词匹配" width="140">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.whole_word}}</div>
          <div v-else>
            <el-select v-model="scope.row.whole_word">
              <el-option value="yes"></el-option>
              <el-option value="no"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="大小写敏感" width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.case_sensitive}}</div>
          <div v-else>
            <el-select v-model="scope.row.case_sensitive">
              <el-option value="yes"></el-option>
              <el-option value="no"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="控制" width="140">
        <template scope="scope">
          <el-row>
            <el-col :span="12"><el-button size="small" type="info" @click="reverseEditState(scope.$index, scope.row)">编辑</el-button>
            </el-col>
            <el-col :span="12"><el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
</template>
<style scoped>

</style>
<script>
  import {Steps, Fields} from "../../model/DataModel";
  import StepCommon from "./mixins/StepMixin";

  export default{
    mixins: [StepCommon],
    data(){
      return {
        step: Steps.ReplaceString()
      }
    },
    methods: {
      updateFields(){
        if (this.step.fields) {
          this.step.outputFields = this.step.fields.map(el => el.out_stream_name);
        }
      },
      fetchFieldsFromInput(){
        this.step.fields = this.step.availableFields.map((el) => {
          return Fields.ReplaceString({in_stream_name: el.name})
        });
      }
    }
  }
</script>
